if ( SERVER ) then

	AddCSLuaFile( "shared.lua" )
	
	SWEP.HoldType			= "physcannon"
	
end

if ( CLIENT ) then

	SWEP.PrintName			= "Gauss Cannon"			
	SWEP.Author				= "Valve"

	SWEP.ViewModelFOV		= 67
end
-----------------------Main functions----------------------------
 
-- function SWEP:Reload() --To do when reloading
-- end 
 
local shootsound = "PropJeep.FireCannon"
local chargesound = "weapon_gauss.chargeloop"
local maxTime = 5
local autoFireTime = 5
local maxDamage = 280
local unchargedDamage = 30
local chargedDamage = 144

function SWEP:Think() -- Called every frame
	local chargingStart = self.Weapon:GetVar("chargingStart")
	if chargingStart then
		local chargingTime = CurTime() - chargingStart
		if not self.Owner:KeyDown(IN_ATTACK2) or chargingTime > autoFireTime then -- <-- here
			self.Weapon:SetVar("chargingStart", nil)
			self.Owner:StopSound(chargesound)
			gaussFire(self.Owner, math.min(maxDamage, maxDamage * (chargingTime / maxTime)))
			self.Weapon:SendWeaponAnim(ACT_VM_SECONDARYATTACK)
			self.Owner:ViewPunch( Angle( -8,0,0 ) )
			self:TakePrimaryAmmo(2)
		else
			local amount = 1 * math.min(1, chargingTime / maxTime)
			self.Owner:ViewPunch( Angle( math.random( -(amount), (amount) ), math.random( -(amount), (amount) ), 0 ) )
		end
	end
end

function SWEP:Initialize()
	util.PrecacheSound(shootsound)
	util.PrecacheSound(chargesound)
	self.charge = 0
	self.set = false
end

function gaussFire(player, damage)
	local trace = player:GetEyeTrace()
	local hit = trace.HitPos

	player:EmitSound(shootsound)
	local MuzzlePos = player:GetShootPos() + (player:GetRight() * Right) + (player:GetUp() * Up) + (player:GetForward() * Forward)
	
	local effectdata = EffectData()
		effectdata:SetOrigin( hit )
		effectdata:SetStart( MuzzlePos )
		effectdata:SetMagnitude(20)
		effectdata:SetScale(5000)
	util.Effect("GaussTracer", effectdata)

	local effectdata3 = EffectData()
		effectdata3:SetOrigin( trace.HitPos )
		effectdata3:SetStart( player:GetShootPos() )
		effectdata3:SetScale(2)
	util.Effect("Sparks", effectdata3)

	util.BlastDamage( player, player, hit, 15, 15 )

	lase(player, MuzzlePos, hit, 3)
	lase(player, MuzzlePos, hit, 0)
end

function lase(player,from, to, noise)
	if SERVER then
	entItem = ents.Create ("info_target")
		entItem:SetKeyValue("targetname", "entItem")
	entItem:Spawn()
	beam = ents.Create("env_laser")
		beam:SetKeyValue("renderamt", "255")
		beam:SetKeyValue("rendercolor", "255 180 180")
		beam:SetKeyValue("texture", "sprites/laserbeam.spr")
		beam:SetKeyValue("TextureScroll", "14")
		beam:SetKeyValue("targetname", "beam" )
		beam:SetKeyValue("renderfx", "2")
		beam:SetKeyValue("width", ".1")
		beam:SetKeyValue("dissolvetype", "None")
		beam:SetKeyValue("EndSprite", "")
		beam:SetKeyValue("LaserTarget", "entItem")
		beam:SetKeyValue("TouchType", "-1")
		beam:SetKeyValue("NoiseAmplitude", "" .. noise)
        beam:Spawn()
	beam:Fire("TurnOn", "", 0.01)
	beam:Fire("kill", "", 0.15)
	entItem:Fire("kill", "", 0.15)
	entItem:SetPos("0, 0, -1")
	beam:SetPos("24, 8, -1")
        end
end
 
function SWEP:PrimaryAttack()
if self.Weapon:Clip1() == 0 then return end
	self.Weapon:SetNextPrimaryFire(CurTime() + .2)
	self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
	self:TakePrimaryAmmo(1)
	
	self.Owner:ViewPunch( Angle( -1,0,0 ) )

	gaussFire(self.Owner, unchargedDamage)
end
 
function SWEP:SecondaryAttack()
	if self.Weapon:GetVar("chargingStart") == nil then
		self.Weapon:SetVar("chargingStart", CurTime())
		self.Weapon:SendWeaponAnim(ACT_VM_SECONDARYATTACK)
		self.Owner:EmitSound(chargesound, 1, 1)

                
	end
end
-------------------------------------------------------------------

------------General Swep Info---------------
SWEP.Author   = "Valve"
SWEP.Contact        = ""
SWEP.Purpose        = "Zapping Homeless"
SWEP.Instructions   = "Primary=Fire Secondary=Charged Shot"
SWEP.Spawnable      = true
SWEP.AdminSpawnable  = false
-----------------------------------------------

------------Models---------------------------
SWEP.ViewModel    = "models/weapons/v_gauss.mdl"
SWEP.WorldModel   = "models/weapons/w_gauss.mdl"
-----------------------------------------------

-------------Primary Fire Attributes----------------------------------------
SWEP.Primary.Delay			= 0.9 	--In seconds
SWEP.Primary.Recoil			= 0.457		--Gun Kick
SWEP.Primary.Damage			= 27.64	--Damage per Bullet
SWEP.Primary.NumShots		= 2		--Number of shots per one fire
SWEP.Primary.Cone			= 0 	--Bullet Spread
SWEP.Primary.ClipSize		= -1	--Use "-1 if there are no clips"
SWEP.Primary.DefaultClip	= -1	--Number of shots in next clip
SWEP.Primary.Automatic   	= true	--Pistol fire (false) or SMG fire (true)
SWEP.Primary.Ammo         	= "Experimental Hyper-Velocitic Ammo"	--Ammo Type
-------------End Primary Fire Attributes------------------------------------
 
-------------Secondary Fire Attributes-------------------------------------
SWEP.Secondary.Delay		= 1.23
SWEP.Secondary.Recoil		= 1.56
SWEP.Secondary.Damage		= 145
SWEP.Secondary.NumShots		= 6
SWEP.Secondary.Cone			= 0
SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic   	= true
SWEP.Secondary.Ammo             = "Experimental Hyper-Velocitic Ammo"	--Ammo Type
-------------End Secondary Fire Attributes--------------------------------